
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "AnimationFramework.h"
#include "ChildView.h"
#include <MMSystem.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif





CString GetErrorString() 
{ 
	// Retrieve the system error message for the last-error code
	LPVOID lpMsgBuf;
	DWORD dw = GetLastError();

	FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

	CString errMsg;
	errMsg.Format( _T("ERROR %d: %s"), dw, lpMsgBuf );
	
	LocalFree(lpMsgBuf);
	return errMsg;
}

// CChildView

CChildView::CChildView() : AnimationThread(33333)
{
	m_bGameOver = false;
	m_bIsPaused = false;
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()



// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	// Do not call CWnd::OnPaint() for painting messages
}

int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	m_pGraphic = CreateGraphic( SCREEN_WIDTH, SCREEN_HEIGHT, 24 );
	
	Start();

	return 0;
}

void CChildView::OnDestroy()
{
	CWnd::OnDestroy();

	// TODO: Add your message handler code here
	Stop();

	DeleteGraphic(m_pGraphic);
	m_pGraphic = NULL;
}

void CChildView::UpdateGame()
{
	if ( m_bIsPaused || m_bGameOver )
		return;
}

void CChildView::RenderGame()
{
	CDC* pDC = GetDC();
	CDC dcMem;
	dcMem.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = dcMem.SelectObject(CBitmap::FromHandle(m_pGraphic->hBitmap));
	dcMem.FillSolidRect( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, RGB(255,128,128) );
	dcMem.SelectObject(pOldBitmap);
	ReleaseDC(pDC);
}

void CChildView::PaintScreen()
{
	CDC* pDC = GetDC();
	CDC dcMem;
	dcMem.CreateCompatibleDC(pDC);

	CBitmap* pOldBitmap = dcMem.SelectObject(CBitmap::FromHandle(m_pGraphic->hBitmap));

	pDC->BitBlt( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
		&dcMem, 0, 0, SRCCOPY );

	dcMem.SelectObject(pOldBitmap);

	ReleaseDC(pDC);
}


void CChildView::Pause()
{
	m_bIsPaused = true;
}

void CChildView::Resume()
{
	m_bIsPaused = false;
}
